package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Behavior;
import com.dnjn.happiness.biz.domain.EmotionType;
import com.dnjn.happiness.biz.service.IAppraiseService;
import com.dnjn.happiness.biz.service.IBehaviorService;
import com.dnjn.happiness.biz.service.impl.EmotionTypeServiceImpl;
import com.dnjn.happiness.common.core.model.ModelHappinessScores;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 行为记录Controller
 *
 * @author happiness
 * @date 2023-10-18
 */
@Tag(name = "biz behavior 行为记录", description = "行为记录")
@RestController
@RequestMapping("/behavior")
public class BehaviorController extends BaseController {
    @Autowired
    private IBehaviorService behaviorService;

    @Autowired
    private EmotionTypeServiceImpl emotionTypeService;

    @Autowired
    private IAppraiseService appraiseService;

    /**
     * 查询行为记录列表
     */
    @RequiresPermissions("biz:behavior:list")
    @PostMapping("/listBack")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult listBack(@RequestBody Behavior behavior, @Parameter(required = true) @RequestParam(defaultValue = "false") Boolean fromFront, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<Behavior> page = Page.of(current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", behavior.getUserId());
        map.put("deptId", behavior.getDeptId());
        if (fromFront) {
            map.put("etype", "NEGATIVE");
        }
        Page<Behavior> list = behaviorService.getPage(page, map);
        return success(list);
    }

    /**
     * wx查询行为记录列表
     */
//    @RequiresPermissions("biz:behavior:list")
    @PostMapping("/list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody Behavior behavior, @Parameter(required = true) @RequestParam(defaultValue = "false") Boolean fromFront, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<Behavior> page = Page.of(current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", behavior.getUserId());
        map.put("deptId", behavior.getDeptId());
        if (fromFront) {
            map.put("etype", "NEGATIVE");
        }
        Page<Behavior> list = behaviorService.getPage(page, map);
        return success(list);
    }

    /**
     * 导出行为记录列表
     */
    @Log(title = "行为记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:behavior:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Behavior behavior) {
        List<Behavior> list = behaviorService.list(Wrappers.query(behavior));
        ExcelUtil<Behavior> util = new ExcelUtil<Behavior>(Behavior.class);
        util.exportExcel(response, list, "行为记录数据");
    }

    /**
     * 获取行为记录详细信息
     */
    @GetMapping(value = "/get/{id}")
    @RequiresPermissions("biz:behavior:query")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get1(@PathVariable("id") Long id) {
        return success(behaviorService.getById(id));
    }


    /**
     * wx获取行为记录详细信息
     */
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(behaviorService.getById(id));
    }

    /**
     * 新增行为记录
     */
    @Log(title = "行为记录", businessType = BusinessType.INSERT)
    @RequiresPermissions("biz:behavior:add")
    @PostMapping("/addBehavior")
    public AjaxResult addBehavior(@RequestBody Behavior behavior) {
        behavior.setCreateBy(SecurityUtils.getUsername());
        return toAjax(behaviorService.save(behavior));
    }

    /**
     * wx新增行为记录
     */
    @Log(title = "行为记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Behavior behavior) {
        behavior.setCreateBy(SecurityUtils.getUsername());
        return toAjax(behaviorService.save(behavior));
    }

    /**
     * 修改行为记录
     */
    @Log(title = "行为记录", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:behavior:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Behavior behavior) {
        return toAjax(behaviorService.updateById(behavior));
    }

    /**
     * 删除行为记录
     */
    @Log(title = "行为记录", businessType = BusinessType.DELETE)
    @RequiresPermissions("biz:behavior:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            behaviorService.lambdaUpdate().eq(Behavior::getId, id).set(Behavior::getDeleted, "1").update();
        }
        return success();
    }

    @GetMapping("/getEmoTionTypeAll")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    @RequiresPermissions("biz:behavior:query")
    public AjaxResult getEmoTionTypeAll() {
        List<EmotionType> emotionTypeList = emotionTypeService.list();
        return AjaxResult.success(emotionTypeList);
    }


    @Operation(summary = "小程序根据用户Id查询用户行为信息列表")
    @GetMapping("/getBehaviorInfoByUserId/{userId}")
    public AjaxResult getBehaviorInfoByUserId(@PathVariable Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        List<Behavior> list = behaviorService.getBehaviorInfoByUserId(userId);
        return AjaxResult.success(list);
    }

    /**
     * 获取行为记录消极记录数-积极记录数
     */
    @Operation(summary = "获取行为记录消极记录数-积极记录数")
    @GetMapping("/getBehaviorTotalByUserId/{userId}")
    public AjaxResult getBehaviorTotalByUserId(@PathVariable Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        List<ModelHappinessScores> list = appraiseService.getAppraiseHappinessScores(userId, "biz_user_behavior");
        return AjaxResult.success(list);
    }
}
